/*
 * @Author: szx
 * @Date: 2021-11-05 09:08:27
 * @LastEditTime: 2021-11-05 09:27:18
 * @Description:
 * @FilePath: \leetcode\1200-1299\1218\1218.cpp
 */
#include <iostream>
#include <unordered_map>
#include <vector>
using namespace std;
// class Solution {
//    public:
//     int longestSubsequence(vector<int> &arr, int difference) {
//         int ans = 0;
//         unordered_map<int, int> dp;
//         for (int v : arr) {
//             dp[v] = dp[v - difference] + 1;
//             ans = max(ans, dp[v]);
//         }
//         return ans;
//     }
// };
class Solution {
   public:
    int longestSubsequence(vector<int> &arr, int difference) {
        int ans = 0;
        int dp[40001]{0};
        for (int num : arr) {
            dp[num + 20000] = dp[num + 20000 - difference] + 1;
            ans = max(ans, dp[num + 20000]);
        }
        return ans;
    }
};
